<!DOCTYPE HTML>
<html lang="en-US" >
    
    <head>
        
        <meta charset="UTF-8">
        <title>局部更新 | Elasticsearch 权威指南</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 1.0.3">
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    
    
    
    <link rel="next" href="../data/mget.html" />
    
    
    <link rel="prev" href="../data/version_control.html" />
    

        
    </head>
    <body>
        
        
<link rel="stylesheet" href="../gitbook/style.css">


        
    <div class="book"  data-level="3.9" data-basepath=".." data-revision="1436390985808">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
    	
    	
    	

        

        
    
        
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                        <i class="fa fa-check"></i>
                        
                         Introduction
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1" data-path="getting_started/README.html">
            
                
                    <a href="../getting_started/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                         入门
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="1.1" data-path="getting_started/what_is_it.html">
            
                
                    <a href="../getting_started/what_is_it.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                         初识
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.2" data-path="getting_started/installing_es.html">
            
                
                    <a href="../getting_started/installing_es.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                         安装
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.3" data-path="getting_started/api.html">
            
                
                    <a href="../getting_started/api.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                         API
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.4" data-path="getting_started/document.html">
            
                
                    <a href="../getting_started/document.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                         文档
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.5" data-path="getting_started/tutorial_indexing.html">
            
                
                    <a href="../getting_started/tutorial_indexing.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                         索引
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.6" data-path="getting_started/tutorial_search.html">
            
                
                    <a href="../getting_started/tutorial_search.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                         搜索
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.7" data-path="getting_started/tutorial_aggregations.html">
            
                
                    <a href="../getting_started/tutorial_aggregations.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                         汇总
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.8" data-path="getting_started/tutorial_conclusion.html">
            
                
                    <a href="../getting_started/tutorial_conclusion.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.8.</b>
                        
                         小结
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.9" data-path="getting_started/distributed.html">
            
                
                    <a href="../getting_started/distributed.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.9.</b>
                        
                         分布式
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="1.10" data-path="getting_started/conclusion.html">
            
                
                    <a href="../getting_started/conclusion.html">
                        <i class="fa fa-check"></i>
                        
                            <b>1.10.</b>
                        
                         本章总结
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="2" data-path="distributed_cluster/README.html">
            
                
                    <a href="../distributed_cluster/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                         分布式集群
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="2.1" data-path="distributed_cluster/empty_cluster.html">
            
                
                    <a href="../distributed_cluster/empty_cluster.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                         空集群
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.2" data-path="distributed_cluster/cluster_health.html">
            
                
                    <a href="../distributed_cluster/cluster_health.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                         集群健康
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.3" data-path="distributed_cluster/add_an_index.html">
            
                
                    <a href="../distributed_cluster/add_an_index.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                         添加索引
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.4" data-path="distributed_cluster/add_failover.html">
            
                
                    <a href="../distributed_cluster/add_failover.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                         容错移转
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.5" data-path="distributed_cluster/scale_horizontally.html">
            
                
                    <a href="../distributed_cluster/scale_horizontally.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                         横向扩展
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.6" data-path="distributed_cluster/scale_more.html">
            
                
                    <a href="../distributed_cluster/scale_more.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                         扩展
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="2.7" data-path="distributed_cluster/coping_with_failure.html">
            
                
                    <a href="../distributed_cluster/coping_with_failure.html">
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                         故障恢复
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="3" data-path="data/README.html">
            
                
                    <a href="../data/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                         数据
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="3.1" data-path="data/document.html">
            
                
                    <a href="../data/document.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                         文档
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.2" data-path="data/index.html">
            
                
                    <a href="../data/index.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                         索引
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.3" data-path="data/get.html">
            
                
                    <a href="../data/get.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                         Get
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.4" data-path="data/exists.html">
            
                
                    <a href="../data/exists.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                         存在
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.5" data-path="data/update.html">
            
                
                    <a href="../data/update.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                         更新
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.6" data-path="data/create.html">
            
                
                    <a href="../data/create.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                         创建
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.7" data-path="data/delete.html">
            
                
                    <a href="../data/delete.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                         删除
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.8" data-path="data/version_control.html">
            
                
                    <a href="../data/version_control.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                         版本控制
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter active" data-level="3.9" data-path="data/partial_update.html">
            
                
                    <a href="../data/partial_update.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.</b>
                        
                         局部更新
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.10" data-path="data/mget.html">
            
                
                    <a href="../data/mget.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.10.</b>
                        
                         Mget
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.11" data-path="data/bulk.html">
            
                
                    <a href="../data/bulk.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.11.</b>
                        
                         Bulk
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="3.12" data-path="data/conclusion.html">
            
                
                    <a href="../data/conclusion.html">
                        <i class="fa fa-check"></i>
                        
                            <b>3.12.</b>
                        
                         总结
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="4" data-path="distributed_crud/README.html">
            
                
                    <a href="../distributed_crud/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                         分布式文档存储
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="4.1" data-path="distributed_crud/routing.html">
            
                
                    <a href="../distributed_crud/routing.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                         路由
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4.2" data-path="distributed_crud/shard_interaction.html">
            
                
                    <a href="../distributed_crud/shard_interaction.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                         主从互通
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4.3" data-path="distributed_crud/create_index_delete.html">
            
                
                    <a href="../distributed_crud/create_index_delete.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                         创建索引删除
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4.4" data-path="distributed_crud/retrieving.html">
            
                
                    <a href="../distributed_crud/retrieving.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                         获取
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4.5" data-path="distributed_crud/partial_updates.html">
            
                
                    <a href="../distributed_crud/partial_updates.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                         局部更新
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4.6" data-path="distributed_crud/bulk_requests.html">
            
                
                    <a href="../distributed_crud/bulk_requests.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                         批量请求
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="4.7" data-path="distributed_crud/bulk_format.html">
            
                
                    <a href="../distributed_crud/bulk_format.html">
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                         批量格式
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="5" data-path="search/README.html">
            
                
                    <a href="../search/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                         搜索
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="5.1" data-path="search/empty_search.html">
            
                
                    <a href="../search/empty_search.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                         空白搜索
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.2" data-path="search/multi_index_multi_type.html">
            
                
                    <a href="../search/multi_index_multi_type.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                         多索引多类型
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.3" data-path="search/pagination.html">
            
                
                    <a href="../search/pagination.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                         分页
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="5.4" data-path="search/query_string.html">
            
                
                    <a href="../search/query_string.html">
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                         查询语句
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    
        
        <li class="chapter " data-level="6" data-path="mapping_analysis/README.html">
            
                
                    <a href="../mapping_analysis/README.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                         映射与统计
                    </a>
                
            
            
            <ul class="articles">
                
    
        
        <li class="chapter " data-level="6.1" data-path="mapping_analysis/exact_vs_full_text.html">
            
                
                    <a href="../mapping_analysis/exact_vs_full_text.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                         Exact_vs_full_text
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.2" data-path="mapping_analysis/inverted_index.html">
            
                
                    <a href="../mapping_analysis/inverted_index.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                         Inverted_index
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.3" data-path="mapping_analysis/analysis.html">
            
                
                    <a href="../mapping_analysis/analysis.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                         Analysis
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.4" data-path="mapping_analysis/mapping.html">
            
                
                    <a href="../mapping_analysis/mapping.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                         Mapping
                    </a>
                
            
            
        </li>
    
        
        <li class="chapter " data-level="6.5" data-path="mapping_analysis/complex_datatypes.html">
            
                
                    <a href="../mapping_analysis/complex_datatypes.html">
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                         Complex_datatypes
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Published using GitBook</a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle font settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Toggle share dropdown"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">Twitter</button>
                <button type="button" data-sharing="google-plus" class="button">Google</button>
                <button type="button" data-sharing="facebook" class="button">Facebook</button>
                <button type="button" data-sharing="weibo" class="button">Weibo</button>
                <button type="button" data-sharing="instapaper" class="button">Instapaper</button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
    
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >Elasticsearch 权威指南</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-gitbook_845">
                    
                        <h1 id="">更新文档中的一部分</h1>
<p>在《更新》一章中，我们讲到了要是想更新一个文档，那么就需要去取回数据，更改数据然后将整个文档进行重新索引。当然，你还可以通过使用<code>更新</code>API来做部分更新，比如增加一个计数器。</p>
<p>正如我们提到的，文档不能被修改，它们只能被替换掉。<code>更新</code>API也<strong>必须</strong>遵循这一法则。从表面看来，貌似是文档被替换了。对内而言，它必须按照<em>找回-修改-索引</em>的流程来进行操作与管理。不同之处在于这个流程是在一个片(shard) 中完成的，因此可以节省多个请求所带来的网络开销。除了节省了步骤，同时我们也能减少多个进程造成冲突的可能性。</p>
<p>使用<code>更新</code>请求最简单的一种用途就是添加新数据。新的数据会被合并到现有数据中，而如果存在相同的字段，就会被新的数据所替换。例如我们可以为我们的博客添加<code>tags</code>和<code>views</code>字段：</p>
<pre><code class="lang-js">POST /website/blog/<span class="hljs-number">1</span>/_update
{
   <span class="hljs-string">"doc"</span> : {
      <span class="hljs-string">"tags"</span> : [ <span class="hljs-string">"testing"</span> ],
      <span class="hljs-string">"views"</span>: <span class="hljs-number">0</span>
   }
}
</code></pre>
<p>如果请求成功，我们就会收到一个类似于<code>索引</code>时返回的内容:</p>
<pre><code class="lang-js">{
   <span class="hljs-string">"_index"</span> :   <span class="hljs-string">"website"</span>,
   <span class="hljs-string">"_id"</span> :      <span class="hljs-string">"1"</span>,
   <span class="hljs-string">"_type"</span> :    <span class="hljs-string">"blog"</span>,
   <span class="hljs-string">"_version"</span> : <span class="hljs-number">3</span>
}
</code></pre>
<p>再次取回数据，你可以在<code>_source</code>中看到更新的结果：</p>
<pre><code class="lang-js">{
   <span class="hljs-string">"_index"</span>:    <span class="hljs-string">"website"</span>,
   <span class="hljs-string">"_type"</span>:     <span class="hljs-string">"blog"</span>,
   <span class="hljs-string">"_id"</span>:       <span class="hljs-string">"1"</span>,
   <span class="hljs-string">"_version"</span>:  <span class="hljs-number">3</span>,
   <span class="hljs-string">"found"</span>:     <span class="hljs-literal">true</span>,
   <span class="hljs-string">"_source"</span>: {
      <span class="hljs-string">"title"</span>:  <span class="hljs-string">"My first blog entry"</span>,
      <span class="hljs-string">"text"</span>:   <span class="hljs-string">"Starting to get the hang of this..."</span>,
      <span class="hljs-string">"tags"</span>: [ <span class="hljs-string">"testing"</span> ], <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-title">1</span>&gt;</span>
      "views":  0 <span class="hljs-tag">&lt;<span class="hljs-title">1</span>&gt;</span>
   }
}</span>
</code></pre>
<ol>
<li>新的数据已经添加到了字段<code>_source</code>中。</li>
</ol>
<h4 id="">使用脚本进行更新</h4>
<hr>
<p>我们将会在《脚本》一章中学习更详细的内容，我们现在只需要了解一些在Elasticsearch中使用API无法直接完成的自定义行为。默认的脚本语言叫做MVEL，但是Elasticsearch也支持JavaScript, Groovy 以及 Python。</p>
<p>MVEL是一个简单高效的JAVA基础动态脚本语言，它的语法类似于Javascript。你可以在<a href="http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html" target="_blank">Elasticsearch scripting docs</a> 以及 <a href="http://mvel.codehaus.org/Getting+Started+for+2.0" target="_blank">MVEL website</a>了解更多关于MVEL的信息。</p>
<hr>
<p>脚本语言可以在<code>更新</code>API中被用来修改<code>_source</code>中的内容，而它在脚本中被称为<code>ctx._source</code>。例如，我们可以使用脚本来增加博文中<code>views</code>的数字：</p>
<pre><code class="lang-js">POST /website/blog/<span class="hljs-number">1</span>/_update
{
   <span class="hljs-string">"script"</span> : <span class="hljs-string">"ctx._source.views+=1"</span>
}
</code></pre>
<p>我们同样可以使用脚本在<code>tags</code>数组中添加新的tag。在这个例子中，我们把新的tag声明为一个变量，而不是将他写死在脚本中。这样Elasticsearch就可以重新使用这个脚本进行tag的添加，而不用再次重新编写脚本了：</p>
<pre><code class="lang-js">POST /website/blog/<span class="hljs-number">1</span>/_update
{
   <span class="hljs-string">"script"</span> : <span class="hljs-string">"ctx._source.tags+=new_tag"</span>,
   <span class="hljs-string">"params"</span> : {
      <span class="hljs-string">"new_tag"</span> : <span class="hljs-string">"search"</span>
   }
}
</code></pre>
<p>获取文档，后两项发生了变化：</p>
<pre><code class="lang-js">{
   <span class="hljs-string">"_index"</span>:    <span class="hljs-string">"website"</span>,
   <span class="hljs-string">"_type"</span>:     <span class="hljs-string">"blog"</span>,
   <span class="hljs-string">"_id"</span>:       <span class="hljs-string">"1"</span>,
   <span class="hljs-string">"_version"</span>:  <span class="hljs-number">5</span>,
   <span class="hljs-string">"found"</span>:     <span class="hljs-literal">true</span>,
   <span class="hljs-string">"_source"</span>: {
      <span class="hljs-string">"title"</span>:  <span class="hljs-string">"My first blog entry"</span>,
      <span class="hljs-string">"text"</span>:   <span class="hljs-string">"Starting to get the hang of this..."</span>,
      <span class="hljs-string">"tags"</span>:  [<span class="hljs-string">"testing"</span>, <span class="hljs-string">"search"</span>], <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-title">1</span>&gt;</span>
      "views":  1 <span class="hljs-tag">&lt;<span class="hljs-title">2</span>&gt;</span>
   }
}</span>
</code></pre>
<ol>
<li><code>tags</code>数组中出现了<code>search</code>。</li>
<li><code>views</code>字段增加了。</li>
</ol>
<p>我们甚至可以使用<code>ctx.op</code>来根据内容选择是否删除一个文档：</p>
<pre><code class="lang-js">POST /website/blog/<span class="hljs-number">1</span>/_update
{
   <span class="hljs-string">"script"</span> : <span class="hljs-string">"ctx.op = ctx._source.views == count ? 'delete' : 'none'"</span>,
    <span class="hljs-string">"params"</span> : {
        <span class="hljs-string">"count"</span>: <span class="hljs-number">1</span>
    }
}
</code></pre>
<h3 id="">更新一篇可能不存在的文档</h3>
<p>想象一下，我们可能需要在Elasticsearch中存储一个页面计数器。每次用户访问这个页面，我们就增加一下当前页面的计数器。但是如果这是个新的页面，我们不能确保这个计数器已经存在。如果我们试着去更新一个不存在的文档，更新操作就会失败。</p>
<p>为了防止上述情况的发生，我们可以使用<code>upsert</code>参数来设定文档不存在时，它应该被创建：</p>
<pre><code class="lang-js">POST /website/pageviews/<span class="hljs-number">1</span>/_update
{
   <span class="hljs-string">"script"</span> : <span class="hljs-string">"ctx._source.views+=1"</span>,
   <span class="hljs-string">"upsert"</span>: {
       <span class="hljs-string">"views"</span>: <span class="hljs-number">1</span>
   }
}
</code></pre>
<p>首次运行这个请求时，<code>upsert</code>的内容会被索引成新的文档，它将<code>views</code>字段初始化为<code>1</code>。当之后再请求时，文档已经存在，所以<code>脚本</code>更新就会被执行，<code>views</code>计数器就会增加。</p>
<h3 id="">更新和冲突</h3>
<p>在本节的开篇我们提到了当<em>取回</em>与<em>重新索引</em>两个步骤间的时间越少，发生改变冲突的可能性就越小。但它并不能被完全消除，在<code>更新</code>的过程中还可能存在另一个进程进行重新索引的可能性。</p>
<p>为了避免丢失数据，<code>更新</code>API会在<em>获取</em>步骤中获取当前文档中的<code>_version</code>，然后将其传递给<em>重新索引</em>步骤中的<code>索引</code>请求。如果其他的进程在这两部之间修改了这个文档，那么<code>_version</code>就会不同，这样更新就会失败。</p>
<p>对于很多的局部更新来说，文档有没有发生变化实际上是不重要的。例如，两个进程都要增加页面浏览的计数器，谁先谁后其实并不重要 —— 发生冲突时只需要重新来过即可。</p>
<p>你可以通过设定<code>retry_on_conflict</code>参数来设置自动完成这项请求的次数，它的默认值是<code>0</code>。</p>
<pre><code class="lang-js">POST /website/pageviews/<span class="hljs-number">1</span>/_update?retry_on_conflict=<span class="hljs-number">5</span> &lt;<span class="hljs-number">1</span>&gt;
{
   <span class="hljs-string">"script"</span> : <span class="hljs-string">"ctx._source.views+=1"</span>,
   <span class="hljs-string">"upsert"</span>: {
       <span class="hljs-string">"views"</span>: <span class="hljs-number">0</span>
   }
}
</code></pre>
<ol>
<li>失败前重新尝试5次</li>
</ol>
<p>这个参数非常适用于类似于增加计数器这种无关顺序的请求，但是还有些情况的顺序就是<strong>很重要</strong>的。例如上一节提到的情况，你可以参考乐观并发控制以及悲观并发控制来设定文档的版本号。</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../data/version_control.html" class="navigation navigation-prev " aria-label="Previous page: 版本控制"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../data/mget.html" class="navigation navigation-next " aria-label="Next page: Mget"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="https://cdn.mathjax.org/mathjax/2.4-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
